package at.jaki.ubb.crawling.components;

import org.apache.log4j.Logger;

import at.jaki.ubb.crawling.api.DataInterpreter;

/**
 * The data interpreter will be used by the Crawling engine though this class.
 * 
 * @author Attila Jakabfi
 * 
 */
public class DataInterpreterWrapper {

	private static final Logger LOG = Logger.getLogger(DataInterpreterWrapper.class);

	private static DataInterpreter dataInterpreter;

	public static synchronized void init(String clazz) {

		DataInterpreter inst = null;
		try {
			Object newInstance = Class.forName(clazz).newInstance();
			inst = (DataInterpreter) newInstance;
		} catch (InstantiationException e) {
			LOG.error("Could not instantiate class ".concat(clazz).concat(". No default constuctor, or the class is abstract."));
			if (LOG.isDebugEnabled()) {
				e.printStackTrace();
			}
		} catch (IllegalAccessException e) {
			LOG.error(e.getClass().getName().concat(" : ") + e.getLocalizedMessage());
			if (LOG.isDebugEnabled()) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			LOG.error(e.getClass().getName().concat(" : ") + e.getLocalizedMessage());
			if (LOG.isDebugEnabled()) {
				e.printStackTrace();
			}
		} catch (RuntimeException e) {
			LOG.error(e.getClass().getName().concat(" : ") + e.getLocalizedMessage());
			if (LOG.isDebugEnabled()) {
				e.printStackTrace();
			}
		}

		dataInterpreter = inst;
	}

	public static DataInterpreter getDataInterpreter() {
		if (dataInterpreter == null) {
			throw new IllegalStateException("data interpretor not initialized");
		}
		return dataInterpreter;
	}
}
